package com.briup.app02.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.app02.bean.Article;
import com.briup.app02.bean.ArticleExample;
import com.briup.app02.bean.extend.ArticleExtend;
import com.briup.app02.dao.ArticleMapper;
import com.briup.app02.dao.extend.ArticleExtendMapper;

import com.briup.app02.service.IArticleService;
import com.briup.app02.utils.CustomerException;

@Service
public class ArticleServiceImpl implements IArticleService {
	
	@Autowired
	private ArticleMapper articleMapper;
	
	@Autowired
	private ArticleExtendMapper articleExtendMapper;
	
//根据id查询所有资讯信息
	@Override
	public List<Article> query(String title,String status,Long categoryId  ){
		ArticleExample example = new ArticleExample();
		ArticleExample.Criteria criteria = example.createCriteria();
		if(title != null) {
			criteria.andTitleLike("%"+title+"%");	
		}
		if(status != null) {
			criteria.andStatusEqualTo(status);
		}
		if(categoryId != null) {
			criteria.andCategoryIdEqualTo(categoryId);
		}
		return articleMapper.selectByExample(example);
	}

//保存或者更新
	@Override
	public void saveOrUpdate(Article article) throws CustomerException{
		if(article.getId()==null) {
			//初始化
			article.setPublishTime(new Date().getTime());
			article.setReadTimes(0l);
			article.setThumpUp(0l);
			article.setStatus("未审核");
			//插入数据库
			articleMapper.insert(article);
		}else {
			articleMapper.updateByPrimaryKey(article);
		}
		
	}	
	
	@Override
	public void deleteById(Long id){
		articleMapper.deleteByPrimaryKey(id);
	}

public List<ArticleExtend> queryCascade(String title, String status, Long categoryId) {
	return articleExtendMapper.select(title,status,categoryId);
}

@Override
public ArticleExtend read(long id) {	
	//根据id查询文章
	return articleExtendMapper.selectById(id);
}

@Override
public void changeStatus(Long id, String status) {
	Article article = articleMapper.selectByPrimaryKey(id);
	article.setStatus(status);
	articleMapper.updateByPrimaryKey(article);
}
}
	
